package com.prodavalnik.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import com.prodavalnik.model.Category;
import com.prodavalnik.model.DataBaseController;
import com.prodavalnik.model.Product;

/**
 * Servlet implementation class getProducts
 */
@WebServlet("/getProducts")
public class getProducts extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public getProducts() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String categoryName = request.getParameter("categoryID");
		Object o = DataBaseController.findCategoryByName(categoryName);
		if(o == null)
			return;
		Category category = (Category)o;
			
		List<Product> products = DataBaseController.getProductFromDB(category.getId());
		
		if(products==null){
			response.getWriter().println("<h1>There are no products with category "+category+" stored in DB</h1>");
			}
		response.getWriter().println(ResponseHTML.createHTMLFromProductList(products,null,"false"));

	}		

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

	}
}
